﻿using BO.Infrastructure.Utils;
using BO.News;
using GUI.Extensions;
using System;
using System.Data;
using System.Text;
using Utilities.Entities;

namespace GUI.Controls.News
{
    public partial class NewsZone2 : System.Web.UI.UserControl
    {
        public int StyleType = 2;
        public int ZoneId { get; set; }
        public string BoxTitle { get; set; }
        public bool DisplayRelateNews = true;
        public bool DisplayPublishedDate = false;
        public int ImageWidth { get; set; }
        public int ImageHeight { get; set; }
        public bool GetDataFromNoCacheLayer = false;
        public bool DisplayOtherNewsAvatar = false;
        public int OtherImageWidth { get; set; }
        public int OtherImageHeight { get; set; }
        public int Top = 2;
        public int TopOther = 3;
        public bool DisplayOtherNews = false;
        private const string LinkFormat = "<a href=\"{0}\" title=\"{2}\">{1}</a>";

        public bool GetDataFromAnyZones = false;
        public int[] AnyZones { get; set; }

        public bool IsThread { get; set; }
        public TagObject Thread { get; set; }


        private static readonly bool EnableCacheHtml = CacheSettingExtensions.EnableCacheHtml;

        protected void Page_Load(object sender, EventArgs e)
        {
            //return;
            var bo = EnableCacheHtml && GetDataFromNoCacheLayer ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();


            if ( IsThread)
            {
                DataTable dt = null;
                DataTable[] arrRelateNews = null;


                if (IsThread && Thread !=null)
                {
                    #region Get data as a thread

                    dt = bo.GetNewsByTag(Thread.Id, 1,  Top + (DisplayOtherNews ? TopOther : 0));
                    BoxTitle = string.Format(LinkFormat, Thread.Url, Thread.Name, PresentationUtils.EnCode(Thread.Name));

                    #endregion
                }
                

                Visible = ctrNewsZoneContent.DataBind(dt, DisplayPublishedDate, DisplayRelateNews, ImageWidth, ImageHeight, numberOfVisibleItem: Top, arrRelateNews: arrRelateNews);

                if (Visible && !string.IsNullOrEmpty(BoxTitle) && dt != null && dt.Rows.Count > 0)
                {
                    if (DisplayOtherNews)
                    {
                        var dtOtherNews = dt.Clone();
                        for (var i = Top; i < dt.Rows.Count; i++)
                        {
                            dtOtherNews.ImportRow(dt.Rows[i]);
                        }
                        ctrRelateNews.Visible = ctrRelateNews.DataBind(dtOtherNews, DisplayOtherNewsAvatar, OtherImageWidth, OtherImageHeight);
                    }
                    ltrHeader.Text = BoxTitle;
                }

            }
            
        }


        internal void DataBind(CategoryObject zone1, CategoryObject zone2, System.Collections.Generic.List<NewsObject> listNews, int imageWidth, int imageHeight)
        {
            var sb = new StringBuilder();
            if (zone1 != null)
            {
                sb.AppendFormat(LinkFormat, zone1.Url, zone1.Name, PresentationUtils.EnCode(zone1.Name));
            }
            if (zone2 != null)
            {
                if (sb.Length > 0)
                {
                    sb.AppendFormat(LinkFormat, "javascript:void(0);", "-", "-");
                }
                sb.AppendFormat(LinkFormat, zone2.Url, zone2.Name, PresentationUtils.EnCode(zone2.Name));
            }
            ltrHeader.Text = sb.ToString();
            ctrNewsZoneContent.DataBind(listNews, imageWidth, imageHeight, true);
        }

        internal void DataBind(CategoryObject zone1, System.Collections.Generic.List<NewsObject> listNews, int imageWidth, int imageHeight)
        {
            if (zone1 != null)
            {
                ltrHeader.Text = string.Format(LinkFormat, zone1.Url, zone1.Name, PresentationUtils.EnCode(zone1.Name));

            }
            ctrNewsZoneContent.DataBind(listNews, imageWidth, imageHeight, true);
        }
    }
}